package ah.html;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit.ParserCallback;

public class Parser extends ParserCallback {
	protected String base;
	protected boolean isLink = false;
	protected boolean isParagraph = false;
	protected boolean isTitle = false;
	protected String htmlbody = new String();
	protected String urlTitle = new String();
	protected Vector<String> links = new Vector<String>();
	protected Vector<String> linkname = new Vector<String>();
	protected String paragraphText = new String();
	protected String linkandparagraph = new String();
	protected String encode = new String();

	protected boolean isInDiv_letrcon1 = false;//ddh 是否在需要的div内
	protected Map<String, String> linksMap = new HashMap<String, String>();//保存链接和名字的
	protected String tempLink = null;
	
	public Parser(String baseurl) {
		base = baseurl;
	}

	public String getEncode() {
		return encode;
	}

	public String getURLtille() {
		return urlTitle;
	}

	public Vector getLinks() {
		return links;
	}

	public Vector getLinkName() {
		return linkname;
	}

	public String getParagraphText() {
		return paragraphText;
	}

	public String getLinknameAndParagraph() {
		return linkandparagraph;
	}
	
	public Map<String, String> getLinksMap() {
		return linksMap;
	}

	public void handleComment(char[] data, int pos) {
	}

	public void handleEndTag(HTML.Tag t, int pos) {
		if (t == HTML.Tag.DIV) {
			isInDiv_letrcon1 = false;
		}
		if (t == HTML.Tag.A) {
			if (isLink) {
				isLink = false;
			}
		} else if (t == HTML.Tag.P) {
			if (isParagraph) {
				isParagraph = false;
			}
		} else if (t == HTML.Tag.TITLE) {
			isTitle = false;
		} else if (t == HTML.Tag.AREA) {
			isLink = false;
		}
	}

	public void handleError(String errorMsg, int pos) {
	}

	public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
		handleStartTag(t, a, pos);
	}

	public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
		tempLink = "";
		// is it some sort of a link
		//获取疾病链接 ddh
		if ( t == HTML.Tag.DIV && "letrcon1".equals(a.getAttribute(HTML.Attribute.CLASS)) ) {
			isInDiv_letrcon1 = true;//判断是否在div-letrcon1内
		} 
		String href = "";
		if (isInDiv_letrcon1==true && (t == HTML.Tag.A) && (t != HTML.Tag.BASE)) {
			href = (String) a.getAttribute(HTML.Attribute.HREF);
			if (href != null) {
				tempLink = href;
				try {
					//URL url = new URL(new URL(base), href);
					links.addElement(href);
					isLink = true;
				} catch (Exception e) {
					// System.out.println(e.getMessage());
				}
			}
		}
		
		/*String href = "";
		if ((t == HTML.Tag.A) && (t != HTML.Tag.BASE)) {
			href = (String) a.getAttribute(HTML.Attribute.HREF);
			if (href != null) {
				try {
					URL url = new URL(new URL(base), href);
					links.addElement(url.toString());
					isLink = true;
				} catch (MalformedURLException e) {
					// System.out.println(e.getMessage());
				}
			}
		} else if (t == HTML.Tag.AREA) {
			href = (String) a.getAttribute(HTML.Attribute.HREF);
			if (href != null) {
				String alt = (String) a.getAttribute(HTML.Attribute.ALT);
				try {
					URL url = new URL(new URL(base), href);
					links.addElement(url.toString());
					if (alt != null) {
						linkname.addElement(alt);
						linkandparagraph += alt;
					}
					isLink = true;
				} catch (MalformedURLException e) {
					// System.out.println(e.getMessage());
				}
			}
		} else if (t == HTML.Tag.TITLE) {
			isTitle = true;
		} else if (t == HTML.Tag.P) {
			isParagraph = true;
		} else if (t == HTML.Tag.BASE) {
			href = (String) a.getAttribute(HTML.Attribute.HREF);
			if (href != null)
				base = href;
		}*/
	}

	public void handleText(char[] data, int pos) {
		if (isLink) {
			String urlname = new String(data);
			
			if (urlname != null) {
				//linkname.addElement(urlname);
				//linkandparagraph += urlname;
				//linksMap.put(urlname, tempLink);
			}
		}
		if (isTitle) {
			String temptitle = new String(data);
			urlTitle = temptitle;
		}
		if (isParagraph) {
			String tempParagraphText = new String(data);
			if (paragraphText != null) {
				paragraphText += tempParagraphText;
				linkandparagraph += tempParagraphText;
			}
		}
	}
}
